package com.jsd.executors;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Executors 工具类、3方法
 */
public class Demo01 {

    public static void main(String[] args) {
        // 单个线程
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        // 创建一个固定的线程池
//        ExecutorService executorService = Executors.newFixedThreadPool(5);
        // 可伸缩，遇强则强 与弱则弱
//        ExecutorService executorService = Executors.newCachedThreadPool();


        try {
            for (int i = 0; i < 10; i++) {
                executorService.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + " ok");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 线程池用完要关闭线程池
            executorService.shutdown();
        }
    }
}
